package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.HairstyleService;
import com.xingkeduo.business.operation.service.ServprodService;
import com.xingkeduo.pojo.query.HairstyleQuery;
import com.xingkeduo.pojo.query.ServprodQuery;
import com.xingkeduo.pojo.table.HairstyleTable;
import com.xingkeduo.pojo.table.ServprodTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class ServprodDomain {

    @Autowired
    private ServprodService servprodService;

    @Autowired
    private HairstyleService hairstyleService;

    /**
     * 查询服务详情
     * @param
     * @return
     * @throws Throwable
     */

    public ServprodTable getServprodById(Long id) throws Throwable {

        if(CommonUtil.isEmpty(id)){
            throw new DataException(DataException.Type.DEFAULT, "id不能为空");
        }
        ServprodTable servprodTable = servprodService.selectById(id);

        return servprodTable;
    }

    public List<ServprodTable> getServprodList(ServprodQuery servprodQuery) throws Throwable {

        List<ServprodTable> servprodList = servprodService.selectLists(servprodQuery);

        return servprodList;
    }

    //分页查询
    public Page<ServprodTable> getServprodPageList(ServprodQuery servprodQuery) throws Throwable {
        Integer pageNo = servprodQuery.getPageNo();
        Integer pageSize = servprodQuery.getPageSize();
        Page<ServprodTable> boardList = servprodService.selectPageLists(servprodQuery,pageNo,pageSize);
        return boardList;
    }


    public int createServprod(ServprodTable servprodTable) throws Throwable {

        if(!CommonUtil.isEmpty(servprodTable.getName())){
            List<ServprodTable> list = servprodService.selectLists(new ServprodQuery().setNameRepeat(servprodTable.getName()));
            if(!CommonUtil.isEmpty(list)){
                throw new DataException(DataException.Type.DEFAULT, "服务名字已经存在");
            }
        }
        int insert = servprodService.insert(servprodTable);
        if(insert>0){
            servprodService.update(new ServprodTable().setId(servprodTable.getId()).setCode(servprodTable.getId()));
        }
        return insert;
    }

    public int modifyServprod(ServprodTable servprodTable) throws Throwable {
       ServprodTable servprodTable1 = servprodService.selectById(servprodTable.getId());
       if(!CommonUtil.isEmpty(servprodTable.getName())){
           if(!servprodTable.getName().equals(servprodTable1.getName())){
               List<ServprodTable> list = servprodService.selectLists(new ServprodQuery().setNameRepeat(servprodTable.getName()));
               if(!CommonUtil.isEmpty(list)){
                   throw new DataException(DataException.Type.DEFAULT, "服务名字已存在");
               }
           }
       }
        return servprodService.update(servprodTable);
    }

    //删除
    public int delServprod(Long id) throws Throwable {

        List<HairstyleTable> hairstyleTableList = hairstyleService.selectHairstyles(new HairstyleQuery().setServprodIds(String.valueOf(id)));

        if(!CommonUtil.isEmpty(hairstyleTableList)){
            throw new DataException(DataException.Type.DEFAULT, "发型库中有绑定的发型，请先解绑！");
        }

        ServprodTable servprodTable = new ServprodTable();

        servprodTable.setId(id);
        servprodTable.setIsDelete(YesNoEnum.YES);
        int delRes = servprodService.delete(servprodTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败！");
        }
    }
}
